package com.example.fszq.volleydemo1;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.bumptech.glide.Glide;
import com.example.fszq.volleydemo1.adapters.CataogryAdapter;
import com.example.fszq.volleydemo1.adapters.IndexFragmentAdapter;
import com.example.fszq.volleydemo1.layout.CircleImageView;
import com.example.fszq.volleydemo1.layout.IndexFragment;
import com.example.fszq.volleydemo1.utils.BitmapCache;
import com.example.fszq.volleydemo1.utils.MyUtil;
import com.example.fszq.volleydemo1.utils.Requesttoserver;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fszq on 2016/1/31.
 */
public class IndexActivity extends AppCompatActivity {
    //FragmentActivity
    private DrawerLayout mDrawerLayout;
    private NavigationView mNavigationView;
    private ActionBarDrawerToggle mDrawerToggle;

    //用户信息相关
    private CircleImageView avatorImg;
    private TextView userName;
    private static final int REQUEST_LOGIN = 3;
    String isLogin = "" ;

    //网络处理有关
    private Requesttoserver Rserver = new Requesttoserver();
    private RequestQueue quence ;

    /*通用类*/
    private MyUtil myUtil = new MyUtil(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index_main);

        quence = Rserver.getRequestQueue(this.getApplicationContext());

        /*处理navigationlayout与view start*/
        Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
        toolbar.setTitle("c2000第三方手机版");

        setSupportActionBar(toolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawer_layout);
        mNavigationView = (NavigationView) findViewById(R.id.id_nv_menu);
        mDrawerLayout.setDrawerShadow(R.mipmap.drawer_shadow, GravityCompat.START);

        //getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_drawer);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setupDrawerContent(mNavigationView);

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,  R.string.drawer_open, R.string.drawer_close){
            @Override
            public void onDrawerClosed(View drawerView) {
                Log.d("drawaction","close");
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {

                isLogin = myUtil.getShared("isLogin");
                //判断是否用户已经登录，如果登录了的话，则下载其头像
                avatorImg = (CircleImageView)findViewById(R.id.avator);
                userName = (TextView)mNavigationView.findViewById(R.id.username);

                if(isLogin.equals("islogin")) {

                    Glide.with(getApplicationContext())
                            .load("http://www.c2000.cn/Images/userface/image1.gif")
                            .error(R.mipmap.ic_account)
                            .into(avatorImg);
                    /*ImageLoader imageLoader = new ImageLoader(quence, new BitmapCache());
                    ImageLoader.ImageListener listener = ImageLoader.getImageListener(avatorImg,
                            R.mipmap.ic_account, R.mipmap.ic_account);
                    imageLoader.get("http://www.c2000.cn/Images/userface/image1.gif", listener);*/
                } else {
                    Bitmap bm = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_account);
                    avatorImg.setImageBitmap(bm);

                }

                //处理用户名等信息的显示
                if (isLogin.equals("notlogin") || isLogin.length() == 0) {
                    userName.setText("请登录");

                    userName.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(IndexActivity.this, UserLoginActivity.class);
                            startActivityForResult(intent, REQUEST_LOGIN);
                            mDrawerLayout.closeDrawers();
                        }
                    });
                }  else {
                    userName.setText(myUtil.getShared("userName"));
                }

                super.onDrawerOpened(drawerView);
            }
        };

        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();

        /*处理navigationlayout与view end*/


        ViewPager viewPager = (ViewPager) findViewById(R.id.indexviewpager);
        IndexFragmentAdapter adapter = new IndexFragmentAdapter(getSupportFragmentManager(),
                this);
        viewPager.setAdapter(adapter);

        //TabLayout
        TabLayout tabLayout = (TabLayout) findViewById(R.id.indextablayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        tabLayout.setupWithViewPager(viewPager);

        //init();
    }

    private void setupDrawerContent(NavigationView navigationView)
    {
        navigationView.setNavigationItemSelectedListener(

                new NavigationView.OnNavigationItemSelectedListener() {

                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) {
                        menuItem.setChecked(true);
                        mDrawerLayout.closeDrawers();
                        return true;
                    }
                });
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode) {
            case REQUEST_LOGIN:
                if(resultCode == RESULT_OK) {
                    userName.setText(myUtil.getShared("userName"));
                    Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

}
